package com.hunlian.makelove.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.R;
import com.hunlian.makelove.bean.IsInfoCompleteBean;
import com.hunlian.makelove.bean.LoginBean;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.common.ContextUtils;
import com.hunlian.makelove.common.StringUtil;
import com.hunlian.makelove.net.NetTools;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.api.IsInfoCompleteServiceVisit;
import com.hunlian.makelove.net.api.LoginServiceVisit;
import com.hunlian.makelove.tools.MD5Tools;
import com.hunlian.makelove.tools.MyToast;
import com.hunlian.makelove.view.CustomProgressDialog;

public class LoginActivity extends BaseActivity{

	private LoginActivity mSelf;
	private Button mBtnLogin;
	private EditText mEtName,mEtPass;
	private CustomProgressDialog progressDialog;
	private TextView mForgetRegister;
	private ContextUtils cu = new ContextUtils();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSelf = this;
		setContentView(R.layout.login);


		//获取网络时间
		NetTools.getNetTime();

		ImageButton mIbBack = (ImageButton) findViewById(R.id.back);
		mIbBack.setVisibility(View.VISIBLE);
		mIbBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mSelf.finish();
			}

		});

		Button rightBtn = (Button) findViewById(R.id.btn_nav_right);
		rightBtn.setVisibility(View.GONE);
		rightBtn.setText("注册");
		rightBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(mSelf,CheckIsRegisterActivity.class);
				intent.putExtra("from", "register");
				startActivity(intent);	
			}
			
		});
		
		TextView mTvTitle = (TextView) findViewById(R.id.title);
		mTvTitle.setText("登录");
		
		mBtnLogin = (Button) findViewById(R.id.btn_login);
		mEtName = (EditText) findViewById(R.id.et_name);
		mEtPass = (EditText) findViewById(R.id.et_password);
		mForgetRegister = (TextView) findViewById(R.id.tv_forget_register);
		setListener();
		String name = AppConfig.getInstance().getLoginedTel();
		if (!TextUtils.isEmpty(name)) {
			mEtName.setText(name);
			mEtName.setSelection(mEtName.length());
		}

		if(StringUtil.isEmpty(mEtName.getText().toString())){
			cu.showInputMethod(mEtName);
		}else if(StringUtil.isEmpty(mEtPass.getText().toString())){
			cu.showInputMethod(mEtPass);
		}

	}
	
	private void setListener() {
		mBtnLogin.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {

				if (TextUtils.isEmpty(mEtName.getText().toString())) {
					Toast.makeText(LoginActivity.this, "请输入手机号", Toast.LENGTH_SHORT)
							.show();
					return;
				}
				if (TextUtils.isEmpty(mEtPass.getText().toString())) {
					Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				userLogin(mEtName.getText().toString(), mEtPass
						.getText().toString());				
			}
			
		});
		
		mForgetRegister.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mSelf,CheckIsRegisterActivity.class);
				intent.putExtra("from", "forgetPassword");
				startActivity(intent);
			}
			
		});
	}
	
	private void userLogin(final String name, final String password) {
		if (!AppApplication.networkAvailable) {
			MyToast.showShort("没有网络");
			return;
		}
		progressDialog = CustomProgressDialog.createDialog(LoginActivity.this,"正在加载中...");
		progressDialog.show();

		LoginServiceVisit visit = new LoginServiceVisit(mSelf);
		visit.setParamters(name, MD5Tools.getInstance().getMD5(password));
		visit.getResult(new NetVisitCallback<LoginBean,Object>(mSelf){

			@Override
			public void onSuccess(LoginBean data) {
				if (progressDialog != null && progressDialog.isShowing()) {
					try {
						progressDialog.dismiss();
						progressDialog = null;
					} catch (Exception e) {
					}
				}
				if(data!=null){
					String userId = data.getUserId();
					AppConfig.authority = data.getAuthority();
					//保存userId到本地
					AppConfig.getInstance().saveLoginedUserExtendInfo(mEtName.getText().toString(),userId);
					//判断是否已完善资料
					requestInfoIsComplete();
				}else{

				}

			}

			@Override
			public void onFailure(String errorEvent, String message,Object obj) {
				super.onFailure(errorEvent, message,obj);
				if (progressDialog != null && progressDialog.isShowing()) {
					try {
						progressDialog.dismiss();
						progressDialog = null;
					} catch (Exception e) {
					}
				}

				//Toast.makeText(mSelf, ExcepitionMsg.ToastError, Toast.LENGTH_SHORT).show();
			}
		});

	}

	/**
	 *这块可以这么做
	 *@author zealjiang
	 *@time 2016/5/17 12:51
	 */
	private void requestInfoIsComplete(){
		if (!AppApplication.networkAvailable) {
			MyToast.showShort("没有网络");
			return;
		}
		IsInfoCompleteServiceVisit visit = new IsInfoCompleteServiceVisit(mSelf);
		final String userId = AppConfig.getInstance().getLoginedUserId();
		visit.setParamters(userId);
		//System.out.println("是否完善资料请求参数：" + visit.getParamter());
		visit.getResult(new NetVisitCallback<IsInfoCompleteBean,Object>(mSelf){

			@Override
			public void onSuccess(IsInfoCompleteBean data) {

				boolean boo;
				if(data==null){
					boo = false;
				}else{
					String isComplete = data.getIsComplete();
					//System.out.println("isComplete :"+isComplete);
					if(isComplete.equals("true")){
						boo = true;
						//将completed标签保存到本地
						AppConfig.getInstance().setCompleted();
					}else{
						boo = false;
					}
				}

				if(boo){
					startActivity(new Intent(mSelf,MainActivity.class));
					mSelf.finish();
				}else{
					startActivity(new Intent(mSelf,OrganizingDataActivity.class));
					mSelf.finish();
				}
			}

			@Override
			public void onFailure(String errorEvent, String message,Object obj) {
				super.onFailure(errorEvent, message,obj);
				
			}
			
		});
	}
	
}
